# -*-coding:utf-8-*-
import sys
import time
import matplotlib

from yolov5 import detect

matplotlib.use("Qt5Agg")  # 声明使用QT5
from PyQt5.QtWidgets import QApplication, QMainWindow
import window
import MyFigure
from PyQt5 import QtWidgets, QtGui, QtCore


# detect.run(weights='yolov5/yolov5s.pt', source='video.mp4',)
def detect_video():
    opt = detect.parse_opt()
    save_dir = detect.main(opt)
    return save_dir


def plot_answer():
    # ===通过graphicview来显示图形
    graphicview = ui.graphicsView  # 第一步，创建一个QGraphicsView
    graphicview.setObjectName("graphicview")
    # 实例化一个MyFigure
    my_figure = MyFigure.MyFigure()
    graphicscene = QtWidgets.QGraphicsScene()  # 第三步，创建一个QGraphicsScene，因为加载的图形（FigureCanvas）不能直接放到graphicview控件中，必须先放到graphicScene，然后再把graphicscene放到graphicview中
    graphicscene.addWidget(my_figure)  # 第四步，把图形放到QGraphicsScene中，注意：图形是作为一个QWidget放到QGraphicsScene中的
    graphicview.setScene(graphicscene)  # 第五步，把QGraphicsScene放入QGraphicsView

    graphicview.show()  # 最后，调用show方法呈现图形！Voila!!

    cpx, cpy = my_figure.plot_Normal()  # 调用MyFigure的画图方法

    text_browser = ui.textBrowser
    text_browser.append("经度" + str(cpx[0]))
    text_browser.append("纬度" + str(cpy[0]))
    cursor = ui.textBrowser.textCursor()
    text_browser.moveCursor(cursor.End)
    # QtWidgets.QApplication.processEvents()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = window.Ui_MainWindow()
    ui.setupUi(MainWindow)
    ui.pushButton.clicked.connect(plot_answer)

    MainWindow.show()
    sys.exit(app.exec_())

    # print(time.time())
    # print(math.floor(time.time()))
    # print(math.ceil(time.time()))
    # time.sleep(1)
    # print(time.time())
    # print(math.floor(time.time()))
    # start = 1
    # end = 2
    # label_dir = detect_video()
    # run_kalman_position(label_dir, start_time=start, end_time=end)
    # axis_transform.get_xy()
